package org.example.ttms.controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.train.Route;
import org.example.ttms.entityClass.train.Station;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddRouteController {

    @FXML
    private StackPane stackPane;

    @FXML
    private Button addButton;

    @FXML
    private ChoiceBox<String> startChoiceBox;

    @FXML
    private ChoiceBox<String> endChoiceBox;

    @FXML
    private ChoiceBox<String> middleChoiceBox;

    @FXML
    private TableView<Station> tableView;

    @FXML
    private TableColumn<Station, String> stationTableColumn;

    @FXML
    private TableColumn<Station, Void> delTableColumn;

    @FXML
    private Label label;

    private List<String> stationList = new ArrayList<>();
    private ObservableList<Station> observableList;
    private HashMap<String, Station> hashMap;

    public StackPane getStackPane() {
        return stackPane;
    }

    public void setStackPane(StackPane stackPane) {
        this.stackPane = stackPane;
    }

    public Button getAddButton() {
        return addButton;
    }

    public void setAddButton(Button addButton) {
        this.addButton = addButton;
    }

    public ChoiceBox<String> getStartChoiceBox() {
        return startChoiceBox;
    }

    public void setStartChoiceBox(ChoiceBox<String> startChoiceBox) {
        this.startChoiceBox = startChoiceBox;
    }

    public ChoiceBox<String> getEndChoiceBox() {
        return endChoiceBox;
    }

    public void setEndChoiceBox(ChoiceBox<String> endChoiceBox) {
        this.endChoiceBox = endChoiceBox;
    }

    public ChoiceBox<String> getMiddleChoiceBox() {
        return middleChoiceBox;
    }

    public void setMiddleChoiceBox(ChoiceBox<String> middleChoiceBox) {
        this.middleChoiceBox = middleChoiceBox;
    }

    public TableView<Station> getTableView() {
        return tableView;
    }

    public void setTableView(TableView<Station> tableView) {
        this.tableView = tableView;
    }

    public TableColumn<Station, String> getStationTableColumn() {
        return stationTableColumn;
    }

    public void setStationTableColumn(TableColumn<Station, String> stationTableColumn) {
        this.stationTableColumn = stationTableColumn;
    }

    public TableColumn<Station, Void> getDelTableColumn() {
        return delTableColumn;
    }

    public void setDelTableColumn(TableColumn<Station, Void> delTableColumn) {
        this.delTableColumn = delTableColumn;
    }

    public Label getLabel() {
        return label;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public List<String> getStationList() {
        return stationList;
    }

    public void setStationList(List<String> stationList) {
        this.stationList = stationList;
    }

    public ObservableList<Station> getObservableList() {
        return observableList;
    }

    public void setObservableList(ObservableList<Station> observableList) {
        this.observableList = observableList;
    }

    public HashMap<String, Station> getHashMap() {
        return hashMap;
    }

    public void setHashMap(HashMap<String, Station> hashMap) {
        this.hashMap = hashMap;
    }

    public void initialize(){
        //填写choiceBox
        Station station = new Station();
        hashMap = station.obtainAllStation();
        for(Map.Entry<String, Station> map : hashMap.entrySet()){
            stationList.add(map.getKey());
        }
        startChoiceBox.getItems().addAll(stationList);
//        startChoiceBox.getSelectionModel().select(0);
        endChoiceBox.getItems().addAll(stationList);
//        endChoiceBox.getSelectionModel().select(1);
        middleChoiceBox.getItems().addAll(stationList);
//        middleChoiceBox.getSelectionModel().select(2);
//        del(startChoiceBox.getValue());
//        del(endChoiceBox.getValue());
//        del(middleChoiceBox.getValue());

        listening();
        //表格
        observableList = FXCollections.observableArrayList();
//        observableList.add(hashMap.get(middleChoiceBox.getValue()));
        stationTableColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
        delTableColumn.setCellFactory(new Callback<TableColumn<Station, Void>, TableCell<Station, Void>>() {
            @Override
            public TableCell<Station, Void> call(TableColumn<Station, Void> stationVoidTableColumn) {
                return new TableCell<Station, Void>(){
                    private final Button delButton = new Button("  删除  ");
                    {
                        Pop_ups popUps = new Pop_ups();
                        delButton.setOnAction(e -> {
                            Station station = getTableView().getItems().get(getIndex());
                                observableList.remove(station);
                                popUps.pop_ups4("删除成功");
                                add(station.getName());
                            System.out.println(observableList);
                        });
                    }
                    @Override
                    protected void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(delButton);
                        }
                    }
                };
            }
        });
        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Station> c) -> {
            label.setVisible(observableList.isEmpty());
        });
        tableView.setItems(observableList);
    }
    //监听三个站点choiceBox
    public void listening(){
        startChoiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                if(oldValue != null)
                    add(oldValue);
                del(newValue);
            }
        });
        endChoiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                if(oldValue != null)
                    add(oldValue);
                del(newValue);
            }
        });
        middleChoiceBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                del(newValue);
                if(newValue != null)
                    observableList.add(hashMap.get(newValue));
            }
        });

    }
    //三个选项框共同删除
    public void del(String str){
        startChoiceBox.getItems().remove(str);
        endChoiceBox.getItems().remove(str);
        middleChoiceBox.getItems().remove(str);
    }
    //三个选项框共同添加
    public void add(String str){
        startChoiceBox.getItems().add(str);
        endChoiceBox.getItems().add(str);
        middleChoiceBox.getItems().add(str);
    }
    //添加路线评判
    public boolean addRouteJudgement(){
        Pop_ups popUps = new Pop_ups();
        if(startChoiceBox.getValue() == null){
            popUps.pop_ups3("未选择起始站");
            return false;
        }
        if(endChoiceBox.getValue() == null){
            popUps.pop_ups3("未选择终点站");
            return false;
        }
        return true;
    }
    @FXML
    void chickAdd(ActionEvent event) {
        if(!addRouteJudgement())
            return ;
        List<String> list = new ArrayList<>();
        list.add(0, startChoiceBox.getValue());
        for(Station station : observableList){
            list.add(station.getName());
        }
        list.add(list.size(), endChoiceBox.getValue());
        Route route = new Route(startChoiceBox.getValue(), endChoiceBox.getValue(), list);
        route.addRoute(route);
        stackPane.getChildren().clear();
        Main.jumpPage2(stackPane, "addRoute.fxml");
    }

}
